package com.cwits.wifi.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.cwits.wifi.R;


/**
 * @author naiyu(http://snailws.com)
 * @version 1.0
 */
public class TasksCompletedView extends View {

    private Handler mHandler;
    //圆环画笔
    private Paint mRoundPaint;
    // 画实心圆的画笔
    private Paint mCirclePaint;
    // 画圆环的画笔
    private Paint mRingPaint;
    // 画字体的画笔
    private Paint mTextPaint;
    // 圆形颜色
    private int mCircleColor;
    // 圆环颜色
    private int mRingColor;
    // 半径
    private float mRadius;
    // 圆环半径
    private float mRingRadius;
    // 圆环宽度
    private float mStrokeWidth;
    // 圆心x坐标
    private int mXCenter;
    // 圆心y坐标
    private int mYCenter;
    // 字的长度
    private float mTxtWidth;
    // 字的高度
    private float mTxtHeight;
    // 总进度
    private int mTotalProgress = 100;
    // 当前进度
    private int mProgress;

    public TasksCompletedView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 获取自定义的属性
        initAttrs(context, attrs);
        initVariable();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.TasksCompletedView, 0, 0);
        mRadius = typeArray.getDimension(R.styleable.TasksCompletedView_radius,
                52);
        mStrokeWidth = typeArray.getDimension(
                R.styleable.TasksCompletedView_strokeWidth, 10);
        mCircleColor =
                typeArray.getColor(R.styleable.TasksCompletedView_circleColor,
                        0xFFFFFFFF);
        mRingColor =
                typeArray.getColor(R.styleable.TasksCompletedView_ringColor,
                        0xFFFFFFFF);

        mRingRadius = mRadius + mStrokeWidth / 2;
    }

    private void initVariable() {
        mHandler = new Handler();

        /**
         * 画最外层的大圆环
         */
        mRoundPaint = new Paint();
        // int centre = getWidth()/2; //获取圆心的x坐标
        //int radius = (int) (centre - roundWidth/2); //圆环的半径
        mRoundPaint.setColor(getResources().getColor(R.color.text_white)); //设置圆环的颜色
        mRoundPaint.setStyle(Paint.Style.STROKE); //设置空心
        mRoundPaint.setStrokeWidth(mStrokeWidth); //设置圆环的宽度
        mRoundPaint.setAntiAlias(true);  //消除锯齿

        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        // mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setColor(getResources().getColor(R.color.transparent));
        mCirclePaint.setStyle(Paint.Style.FILL);

        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(getResources().getColor(R.color.processColor));
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        // mTextPaint.setARGB(255, 255, 255, 255);
        mTextPaint.setColor(getResources().getColor(R.color.processColor));
        mTextPaint.setTextSize(mRadius * 3 / 4);

        FontMetrics fm = mTextPaint.getFontMetrics();
        mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        mXCenter = getWidth() / 2;
        mYCenter = getHeight() / 2;

        canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
        canvas.drawCircle(mXCenter, mYCenter, mRadius + mStrokeWidth / 2, mRoundPaint); //画出圆环

        if (mProgress > 0) {
            RectF oval = new RectF();
            oval.left = (mXCenter - mRingRadius);
            oval.top = (mYCenter - mRingRadius);
            oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
            oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
            canvas.drawArc(oval, -90,
                    ((float) mProgress / mTotalProgress) * 360, false,
                    mRingPaint); //
            // canvas.drawCircle(mXCenter, mYCenter, mRadius + mStrokeWidth / 2,
            // mRingPaint);
            String txt = mProgress + "%";
            mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());
            canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter
                    + mTxtHeight / 4, mTextPaint);
        }
    }

    public void setProgress(int progress) {
        mProgress = progress;
        // invalidate();
        postInvalidate();
    }

    public void startProgress(int progress) {
        if (progress >= 100)
            return;
        mProgress = progress;
        postInvalidate();
        mHandler.postDelayed(ProgressRunnable, 1000);
    }

    public void stopProgress() {
        mHandler.removeCallbacks(ProgressRunnable);
    }

    private Runnable ProgressRunnable = new Runnable() {

        @Override
        public void run() {
            if (mProgress >= 100) {
                mProgress = 100;
                postInvalidate();
            } else {
                mProgress = mProgress + 1;
                postInvalidate();
                mHandler.postDelayed(ProgressRunnable, 1000);
            }
        }
    };

}
